package com.ultrasoft.farmasoft.repository;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ultrasoft.farmasoft.domain.Dosage;

@Repository(value = "dosageDao")
@Transactional(readOnly = false)
public class DosageDaoImpl implements DosageDao {

	private EntityManager em = null;
	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;
	}
	
	public List<Dosage> getListDosage() {
		TypedQuery<Dosage> q = em.createNamedQuery("Dosage.findAll",Dosage.class);
    	return q.getResultList();
	}
	public List<Dosage> getListDosageByPharmacyIdOrderBy(Integer pharmacyId) {
		TypedQuery<Dosage> q = em.createNamedQuery("Dosage.findByPharmacyIdOrderBy",Dosage.class);
    	q.setParameter("pharmacyId",pharmacyId);
    	return q.getResultList();
	}
	
	public List<Dosage> getListDosageByOrderBy(short state) {
		TypedQuery<Dosage> q = em.createNamedQuery("Dosage.findByState",Dosage.class);
    	q.setParameter("state",state);
    	return q.getResultList();
	}
	
	
	
	
	public List<Dosage> getListDosageByPharmacyIdByStateByCurrentDate(Integer pharmacyId,short state) {
		TypedQuery<Dosage> q = em.createNamedQuery("Dosage.findByPharmacyIdByStateByCurrentDate",Dosage.class);
    	q.setParameter("pharmacyId",pharmacyId);
    	q.setParameter("state",state);
    	return q.getResultList();
	}
	public List<Dosage> getListDosageByPharmacyIdByState(Integer pharmacyId, short state){
		TypedQuery<Dosage> q = em.createNamedQuery("Dosage.findByPharmacyIdByState",Dosage.class);
    	q.setParameter("pharmacyId",pharmacyId);
    	q.setParameter("state",state);
    	return q.getResultList();
	}

	
	
	
	public List<Dosage> getListDosageByPharmacyIdByStateOrderBy(Integer pharmacyId, short state){
		TypedQuery<Dosage> q = em.createNamedQuery("Dosage.findByPharmacyIdByStateOrderBy",Dosage.class);
    	q.setParameter("pharmacyId",pharmacyId);
    	q.setParameter("state",state);
    	return q.getResultList();
	}
	public Dosage findDosageByDosageId(Integer dosageId) {
		Dosage res=null;
		try {
	    	res=em.find(Dosage.class,dosageId);
		} catch (Exception e) {}
    	return res;
	}
	public void remove(Dosage dosage) {
		em.remove(em.getReference(Dosage.class,dosage.getDosageId()));
	}
	public void update(Dosage dosage) {
		em.merge(dosage);
	}
	public void add(Dosage dosage) {
		em.persist(dosage);
	}
	
	
	
	public boolean verificarDosificacion(Integer pharmacyId){
		boolean res = true;
        int ultimoNroFactura = 0;
		List<Dosage> dosageList = getListDosageByPharmacyIdByStateByCurrentDate(pharmacyId,(short)1);
		if(!dosageList.isEmpty()){
			ultimoNroFactura = dosageList.get(0).getLastNumBillIssued();
			ultimoNroFactura++;
		}else{
			int auxUpdate = 0;
			List<Dosage> dosageSearchOneList = getListDosageByPharmacyIdByState(pharmacyId,(short)1);
			if(!dosageSearchOneList.isEmpty()){
				Dosage dosageSearchOne = dosageSearchOneList.get(0);
				dosageSearchOne.setState((short)0);
				em.merge(dosageSearchOne);
			}else{ // agregue yo
				System.out.println(" dosageSearchOneList vaciooooooooooooooooooooooooooooooooooooooooooooooooooooooo");
			}
			List<Dosage> dosageSearchTwoList = getListDosageByPharmacyIdByState(pharmacyId,(short)2);
			if(!dosageSearchTwoList.isEmpty()){
				Dosage dosageSearchTwo = dosageSearchTwoList.get(0);
				dosageSearchTwo.setState((short)1);
				em.merge(dosageSearchTwo);
				auxUpdate = 1;
			}else{ // agregue yo
				System.out.println(" dosageSearchTwoList vaciooooooooooooooooooooooooooooooooooooooooooooooooooooooo");
			}
			if(auxUpdate == 0){
				res = false;
				System.out.println("AdminDosificacion -> verificarDosificacion(int idEmpresa) El sistema no puede emitir facturas, vencio la fecha limite de emision o no existe numero de facturas");
			}else{
				List<Dosage> dosage1List = getListDosageByPharmacyIdByStateByCurrentDate(pharmacyId,(short)1);
				if(!dosage1List.isEmpty()){
					Dosage dosage1 = dosage1List.get(0);
					ultimoNroFactura = dosage1.getLastNumBillIssued();
					ultimoNroFactura++;
				}else{ // agregue yo
					System.out.println(" dosage1List vaciooooooooooooooooooooooooooooooooooooooooooooooooooooooo");
				}
			}
		}
		if(res){
			List<Dosage> dosage2List = getListDosageByPharmacyIdByStateByCurrentDate(pharmacyId,(short)1);
			if(!dosage2List.isEmpty()){
				Dosage dosage2 = dosage2List.get(0);
				dosage2.setLastNumBillIssued(ultimoNroFactura);
				em.merge(dosage2);
			}else{ // agregue yo
				System.out.println(" dosage2List vaciooooooooooooooooooooooooooooooooooooooooooooooooooooooo");
			}
		}
		return res;
	}
}
